package com.msmall.community.common.service.impl;

import java.util.Date;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.msmall.base.service.impl.BaseManagerImpl;
import com.msmall.base.util.ConstantDB;
import com.msmall.community.common.dao.MessageDao;
import com.msmall.community.common.model.Message;
import com.msmall.community.common.service.MessageManager;

public class MessageManagerImpl extends BaseManagerImpl<Message, Long> implements MessageManager{
	
	private static final Log log = LogFactory.getLog(MessageManagerImpl.class);
	
	private MessageDao messageDao;

	public void setMessageDao(MessageDao messageDao) {
		this.messageDao = messageDao;
	}

	public List<Message> getMessages(Long sender, Long receiver) {
		try{
			return messageDao.getMessages(sender, receiver);
		}catch(Exception e){
			log.error(e);
		}
		return null;
	}
	public List<Message> getMessages(Long sender, Long receiver, Integer type){
		try{
			return messageDao.getMessages(sender, receiver, type);
		}catch(Exception e){
			log.error(e);
		}
		return null;
	}
	/**
	 * 获得用户的所有消息
	 */
	public List<Message> getMessagesByUserid(Long receiver){
		try{
			return messageDao.getMessagesByUserid(receiver);
		}catch(Exception e){
			log.error(e);
		}
		return null;
	}
	
	public void deleteMessage(Long sender, Long receiver) {
		try{
			List<Message> messages = getMessages(sender, receiver);
			messageDao.removeAll(messages);
		}catch(Exception e){
			log.error(e);
		}
	}
	/**
	 * 将消息状态设置为已读
	 * @param messageid
	 */
	public void setRead(Long messageid){
		try{
			Message message = (Message) get(Message.class, messageid);
			message.setStatus(ConstantDB.READ);
			saveObject(message);
		}catch(Exception e){
			log.error(e);
		}
	}
	
	/**
	 * 好友请求时，发送系统消息给用户
	 * 单向在任何时候只存在一条好友请求
	 */
	public void askMessage(Long sender, Long receiver, String content){
		List<Message> messages = getMessages(sender, receiver, ConstantDB.MESSAGE_TYPES_ROSTER_REQUEST);
		messageDao.removeAll(messages);
		
		Message message = new Message();
		message.setContent(content);
		message.setCreateTime(new Date());
		message.setReceiver(receiver);
		message.setSender(sender);
		message.setStatus(ConstantDB.UNREAND);
		message.setTitle(ConstantDB.MESSAGE_CONTENT_ROSTER);
		message.setTypes(ConstantDB.MESSAGE_TYPES_ROSTER_REQUEST);
		messageDao.saveObject(message);
	}
	/**
	 * 同意好友请求时，发送系统消息给用户
	 */
	public void acceptMessage(Long sender, Long receiver){
		Message message = new Message();
		message.setContent("");
		message.setCreateTime(new Date());
		message.setReceiver(receiver);
		message.setSender(sender);
		message.setStatus(ConstantDB.UNREAND);
		message.setTitle(ConstantDB.MESSAGE_CONTENT_ROSTER);
		message.setTypes(ConstantDB.MESSAGE_TYPES_ROSTER_ACCEPT);
		messageDao.saveObject(message);
	}
	/**
	 * 拒绝好友请求时，发送系统消息给用户
	 */
	public void rejectMessage(Long sender, Long receiver){
		Message message = new Message();
		message.setContent("");
		message.setCreateTime(new Date());
		message.setReceiver(receiver);
		message.setSender(sender);
		message.setStatus(ConstantDB.UNREAND);
		message.setTitle(ConstantDB.MESSAGE_CONTENT_ROSTER);
		message.setTypes(ConstantDB.MESSAGE_TYPES_ROSTER_REJECT);
		messageDao.saveObject(message);
	}
	/**
	 * 组长/副组长 邀请用户，被邀请人接受到消息
	 * 格式参考 Message 说明
	 */
	public void inviteTeamMessage(Long senderid, Long receiverid, Long teamid){
		Message message = new Message();
		message.setContent(String.valueOf(teamid));
		message.setCreateTime(new Date());
		message.setReceiver(receiverid);
		message.setSender(senderid);
		message.setStatus(ConstantDB.UNREAND);
		message.setTitle(ConstantDB.MESSAGE_TITLE_TEAM_INVITE);
		message.setTypes(ConstantDB.MESSAGE_TYPES_TEAM_INVITE);
		messageDao.saveObject(message);
	}
	/**
	 * 用户申请加入群组时，通知组长/副组长
	 */
	public void joinTeamMessage(Long senderid, List<Long> receivers, Long teamid){
		for(int i=0; i<receivers.size();i ++){
			Long receiverid = receivers.get(i);
			Message message = new Message();
			message.setContent(String.valueOf(teamid));
			message.setCreateTime(new Date());
			message.setReceiver(receiverid);
			message.setSender(senderid);
			message.setStatus(ConstantDB.UNREAND);
			message.setTitle(ConstantDB.MESSAGE_TITLE_TEAM_INVITE);
			message.setTypes(ConstantDB.MESSAGE_TYPES_TEAM_JOIN);
			messageDao.saveObject(message);
		}
	}
	
	/**
	 * 组长/副组长 通过用户申请,通知用户
	 */
	public void joinTeamAcceptMessage(Long senderid, Long receiverid, Long teamid){
		Message message = new Message();
		message.setContent(String.valueOf(teamid));
		message.setCreateTime(new Date());
		message.setReceiver(receiverid);
		message.setSender(senderid);
		message.setStatus(ConstantDB.UNREAND);
		message.setTitle(ConstantDB.MESSAGE_TITLE_TEAM_INVITE);
		message.setTypes(ConstantDB.MESSAGE_TYPES_TEAM_JOIN_ACCEPT);
		messageDao.saveObject(message);
	}
	
	/**
	 * 组长/副组长 拒绝用户申请,通知用户
	 */
	public void joinTeamRejectMessage(Long senderid, Long receiverid, Long teamid){
		Message message = new Message();
		message.setContent(String.valueOf(teamid));
		message.setCreateTime(new Date());
		message.setReceiver(receiverid);
		message.setSender(senderid);
		message.setStatus(ConstantDB.UNREAND);
		message.setTitle(ConstantDB.MESSAGE_TITLE_TEAM_INVITE);
		message.setTypes(ConstantDB.MESSAGE_TYPES_TEAM_JOIN_REJECT);
		messageDao.saveObject(message);
	}
	
	/**
	 * 用户退出,通知用户
	 */
	public void leaveTeamUserMessage(Long receiverid, Long teamid){
		Message message = new Message();
		message.setContent(String.valueOf(teamid));
		message.setCreateTime(new Date());
		message.setReceiver(receiverid);
		message.setStatus(ConstantDB.UNREAND);
		message.setTitle(ConstantDB.MESSAGE_TITLE_TEAM_INVITE);
		message.setTypes(ConstantDB.MESSAGE_TYPES_TEAM_LEAVE_USER);
		messageDao.saveObject(message);
	}
	/**
	 * 组长修改了组员/副组长 职位
	 */
	public void teamPositionMessage(Long senderid, Long receiverid, Long teamid){
		Message message = new Message();
		message.setContent(String.valueOf(teamid));
		message.setCreateTime(new Date());
		message.setSender(senderid);
		message.setReceiver(receiverid);
		message.setStatus(ConstantDB.UNREAND);
		message.setTitle(ConstantDB.MESSAGE_TITLE_TEAM_INVITE);
		message.setTypes(ConstantDB.MESSAGE_TYPES_TEAM_POSITION);
		messageDao.saveObject(message);
	}
}
